import api from '../../utils/api';
import {
  getPage
} from '../../utils/config';
import wxApi from '../../utils/wxApi';
import {
  http,
  $empty,
  $toast,
  $loading,
  Validate
} from '../../utils/utils.js';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    contacter: '',
    username: '',
    id: 0,
    firstEye: true,
    firstIcon: 'closed-eye',
    secondEye: true,
    secondIcon: 'closed-eye',
    firstWord: '',
    secondWord: ''
  },


  /**
   * 第一次密码 input
   */
  contacterInput(event) {
    this.setData({
      contacterError: '',
      contacter: $empty(event.detail)
    });
  },

  /**
   * 第二次密码 input
   */
  usernameInput(event) {
    this.setData({
      usernameError: '',
      username: $empty(event.detail)
    });
  },

  /**
   * 密码切换显示first
   */
  firstChange() {
    let {
      firstIcon,
      firstEye
    } = this.data;
    if (firstEye) {
      firstIcon = 'eye-o';
      firstEye = false;
    } else {
      firstIcon = 'closed-eye';
      firstEye = true;
    }

    this.setData({
      firstIcon,
      firstEye
    })
  },

  /**
   * 密码切换显示second
   */
  secondChange() {
    let {
      secondIcon,
      secondEye
    } = this.data
    if (secondEye) {
      secondIcon = 'eye-o';
      secondEye = false;
    } else {
      secondIcon = 'closed-eye';
      secondEye = true;
    }

    this.setData({
      secondIcon,
      secondEye
    })
  },

  /**
   * 第一次密码 input
   */
  firstPasswordInput(event) {
    this.setData({
      firstPasswordError: '',
      firstWord: $empty(event.detail)
    });
  },

  /**
   * 第二次密码 input
   */
  secondPasswordInput(event) {
    this.setData({
      secondPasswordError: '',
      secondWord: $empty(event.detail)
    });
  },

  /**
   * 密码修改--网络请求
   */
  contacterSubmit(e) {
    const {
      value
    } = e.detail;
    Validate.check(this, this.passwordValidate, value, (res) => {
      if (!res) {
        return false;
      }
      if (value.firstWord != value.secondWord) {
        $toast(`两次密码不一致`, {
          icon: 'fail',
          times: 1000,
        })
        return false;
      }
      const {
        shopid,
        id
      } = this.data;

      http.post(api.default, {
        data: {
          action: 'editadmin',
          contacter: value.contacter,
          username: value.username,
          pwd: value.firstPassword,
          shopid: shopid,
          userid: id
        }
      }).then((res) => {
        //修改成功跳转
        $toast(`修改成功`, {
          icon: 'success',
          times: 1000,
        }).then(() => {
          wxApi.refreshPage(getPage.admin);
        })
      });
    });
  },
  delete() {
    const {
      shopid,
      id
    } = this.data;
    if (id && shopid > 0) {
      http.post(api.default, {
        data: {
          action: 'deleteadmin',
          shopid: shopid,
          id: id
        }
      }).then((res) => {
        //修改成功跳转
        $toast(`删除成功`, {
          icon: 'success',
          times: 1000,
        }).then(() => {
          wxApi.refreshPage(getPage.admin);
        })
      });

    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取用户登录
    app.getLogin().then((res) => {
      const {
        shop
      } = res.data;
      this.setData({
        shopid: shop.ShopId
      })
    })

    Validate.rules("#contacter,#username,#firstPassword,#secondPassword", (res) => {
      this.passwordValidate = res;
    });
    if (JSON.stringify(options) !== '{}') {
      this.setData({
        id: options.id
      });
      http.post(api.default, {
        data: {
          action: 'getadmininfo',
          id: options.id
        }
      }).then((res) => {
        const data = res.data.data;
        this.setData({
          contacter: data.real_name,
          username: data.user_name
        });
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})